<template>
  <div>
    <!-- Header 区域 -->
    <van-nav-bar
      fixed
      title="文章详情"
      left-arrow
      @click-left="$router.push('/')"
    />

    <!-- 文章信息区域 -->
    <div class="article-container">
      <!-- 文章标题 -->
      <h1 class="art-title">{{ article.title }}</h1>

      <!-- 用户信息 -->
      <van-cell
        center
        :title="article.aut_name"
        :label="formatTime(article.pubdate)"
      >
        <template #icon>
          <img :src="article.aut_photo" alt="" class="avatar" />
        </template>
        <template #default>
          <div>
            <!--
              1. 找到一个关键的字段 控制同一时间只显示一个按钮
              2. 点击事件 判断如果当前已关注取消关注  如果未关注 进行关注
             -->
            <van-button
              type="info"
              size="mini"
              v-if="article.is_followed"
              @click="follow"
              >已关注</van-button
            >
            <van-button
              icon="plus"
              type="info"
              size="mini"
              plain
              v-else
              @click="follow"
              >关注</van-button
            >
          </div>
        </template>
      </van-cell>

      <!-- 分割线 -->
      <van-divider></van-divider>

      <!-- 文章内容 -->
      <div class="art-content" v-html="article.content"></div>

      <!-- 分割线 -->
      <van-divider>End</van-divider>

      <!-- 点赞 -->
      <div class="like-box">
        <van-button
          @click="changeAttitute"
          icon="good-job"
          type="danger"
          size="small"
          v-if="article.attitude === 1"
          >已点赞</van-button
        >
        <van-button
          @click="changeAttitute"
          icon="good-job-o"
          type="danger"
          plain
          size="small"
          v-if="article.attitude === -1"
          >点赞</van-button
        >
      </div>
    </div>
  </div>
</template>

<script>
import { articleDetailAPI, followAutAPI, unFollowAutAPI, like, unLike } from '@/api'
import { formatRelitiveTime } from '@/utils/time'
export default {
  name: 'detail-index',
  data () {
    return {
      article: {}
    }
  },
  mounted () {
    this.fetchDetail()
  },
  activated () {
    this.fetchDetail()
  },
  methods: {
    async fetchDetail () {
      const id = this.$route.query.id
      const res = await articleDetailAPI(id)
      this.article = res.data
    },
    formatTime: formatRelitiveTime,
    follow () {
      // 判断当前是否已经关注 调用俩个接口
      if (this.article.is_followed) {
        // 取消关注
        unFollowAutAPI(this.article.aut_id)
      } else {
        // 关注
        followAutAPI(this.article.aut_id)
      }
      // 这个做法可以做到更新 但是为了一个小按钮的状态 拉取了整个全部接口信息 代码太大
      // this.fetchDetail()
      // 乐观更新: 不通过调用接口 直接修改本地状态 达到及时同步
      this.article.is_followed = !this.article.is_followed
    },
    changeAttitute () {
      if (this.article.attitude === 1) {
        // 取消点赞
        unLike(this.article.art_id)
      } else {
        // 点赞
        like(this.article.art_id)
      }
      this.article.attitude = this.article.attitude * -1
    }
  }
}
</script>

<style scoped lang="less">
.article-container {
  padding: 10px;
  margin-top: 46px;
}
.art-title {
  font-size: 16px;
  font-weight: bold;
  margin: 10px 0;
}

.art-content {
  font-size: 12px;
  line-height: 24px;
  width: 100%;
  overflow-x: scroll;
  word-break: break-all;
  /deep/ img {
    width: 100%;
  }
  /deep/ pre {
    white-space: pre-wrap;
    word-wrap: break-word;
  }
}

.van-cell {
  padding: 5px 0;
  &::after {
    display: none;
  }
}

.avatar {
  width: 60px;
  height: 60px;
  border-radius: 50%;
  background-color: #f8f8f8;
  margin-right: 5px;
  border: none;
}

.like-box {
  display: flex;
  justify-content: center;
}
</style>
